<?php

namespace app\middleware;

use app\model\Supplier;
use Closure;
use think\facade\Cache;
use think\Request;

class OpenMiddler
{
    public function handle(Request $request, Closure $next)
    {
        $filed = $this->setField($request->controller(), $request->action());
        $param = $request->param();
        if (!empty($filed['checkField'])) {
            foreach ($filed['checkField'] as $v) {
                if (!isset($param[$v])) throw new \Exception("参数{$v}不能为空！", 4095);
                if ($param[$v] == null || $param[$v] == '') throw new \Exception("参数{$v}不能为空！", 4095);
            }
        }
        if ($filed['checkAuthorization'] && empty($request->header('Authorization'))) throw new \Exception("接口鉴权失败！", 4096);
        else if ($filed['checkAuthorization']) {
            $author = $request->header('Authorization', '[]');
            [$type, $appid, $token] = explode(" ", $author);
            if (!Cache::has("{$appid}_token")) throw new \Exception("接口鉴权失败！", 4096);
            $access_toke = Cache::get("{$appid}_token");
            if ($access_toke['access_token'] != $token) throw new \Exception("接口鉴权失败！", 4096);
            $supplier = app(Supplier::class)->where('appid', $appid)->where('state', 1)->find();
            if (empty($supplier)) throw new \Exception("渠道账号不存在！", 7044);
            $request->supplier = $supplier;
        }
//        获取接口请求时间是否超出5分钟
//        if ($param['timestamp'] < time() - 300) throw new \Exception("接口请求时间超出5分钟！", 4097);
        return $next($request);
    }

    protected function setField($controller, $action): array
    {
        $field = [
            "open.Auth" => [
                "token" => [
                    "checkField" => ["appid", "timestamp", "sign"],
                    "checkAuthorization" => false
                ],
                "refresh" => [
                    "checkField" => ["appid", "timestamp", "refresh_token", "sign"],
                    "checkAuthorization" => false
                ],
            ],
            "open.Station" => [
                "lst" => [
                    "checkField" => ["timestamp"],
                    "checkAuthorization" => true
                ]
            ],
            "open.Payment" => [
                "discount" => [
                    "checkField" => ["oil", "timestamp", "money", "station_id"],
                    "checkAuthorization" => true
                ],
                "create" => [
                    "checkField" => ["oil", "timestamp", "money", "station_id", "out_order_sn"],
                    "checkAuthorization" => true
                ],
                "submit" => [
                    "checkField" => ["order", "timestamp"],
                    "checkAuthorization" => true
                ],
                "refund" => [
                    "checkField" => ["order", "reason", "timestamp"],
                    "checkAuthorization" => true
                ],
                "detail" => [
                    "checkAuthorization" => true
                ]
            ],
        ];
        return $field[$controller][$action] ?? [];
    }
}
